package com.thams.junit;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * 数据操作工厂，所有的数据操作都从该工厂中获得。
 * 
 * @author 刘玉华
 * @time 2007-12-14
 */

public class ServiceFactory {
	private static BeanFactory factory = null;
	private static Logger log = Logger.getLogger(ServiceFactory.class);
	static {
		try {
			String classPath = ServiceFactory.class.getClassLoader()
					.getResource("").getPath();
			String[] basexml = new String[] {
					"file:WebRoot/WEB-INF/spring/base-config.xml"
					,"file:WebRoot/WEB-INF/spring/dao-config.xml"
					,"file:WebRoot/WEB-INF/spring/service-config.xml"
					,"file:WebRoot/WEB-INF/spring/scheduler-config.xml"
					,"file:WebRoot/WEB-INF/spring/aop-config.xml" 
					};
//			String[] servicexml = new String[] {
//					"/config/spring_config/spring_service.xml",
//					"/config/spring_config/junit/spring_service.xml" };
//			String[][] xmlFiles = new String[][] { basexml, servicexml };
//			for (String[] xmlfiles : xmlFiles) {
//				String s = StringUtils.replace(FileUtils.readFileToString(new File(classPath + xmlfiles[0]),"utf-8"),
//						"<flex:remoting-destination/>", "");
//				s = StringUtils.replace(s, "spring_config/spring_service.xml", "spring_config/junit/spring_service.xml");
//				FileUtils.writeStringToFile(new File(classPath + xmlfiles[1]),s,"utf-8");
//
//			}
			FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(basexml);
//				 new FileSystemXmlApplicationContext("file:WebRoot/WEB-INF/spring/*-config.xml");
			factory = (BeanFactory) context;
		} catch (Exception e) {
			e.printStackTrace();
			log.error(ServiceFactory.class + e.getMessage());
		}
	}

	/**
	 * <li>获取service服务
	 * 
	 * @param beanName
	 *            名称
	 * @return (T)service服务
	 */
	public static Object getBean(String beanName) {
		Object obj = null;
		try {
			obj = (Object) factory.getBean(beanName);
		} catch (Exception e) {
			log.error(ServiceFactory.class + e.getMessage()
					+ "无法创建service服务,请检查spring配置文件!");
		}
		return obj;
	}


	public static void main(String[] args) {
//		FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
//				new String[] { "file:WebRoot/WEB-INF/spring/base-config.xml" });
		System.out.println(getBean("xtgnDao"));
	}
}
